package com.yorick.file.controller;

import com.yorick.file.modal.request.*;
import com.yorick.file.service.FileService;
import com.yorick.login.enums.Account;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("file")
public class FileController {
    @Resource
    FileService fileService;

    @PostMapping(value = "createFile")
    public Map<String, Object> createFile(@RequestBody CreateFileRequest requestBody, HttpServletRequest request){
        HttpSession session = request.getSession();
        int aid = Integer.parseInt(session.getAttribute(Account.aid.name()).toString());
        return fileService.createFile(requestBody, aid);
    }

    @PostMapping(value = "fileList")
    public Map<String, Object> fileList(@RequestBody FileListRequest requestBody, HttpServletRequest request){
        HttpSession session = request.getSession();
        int aid = Integer.parseInt(session.getAttribute(Account.aid.name()).toString());
        return fileService.fileList(requestBody, aid);
    }

    @PostMapping(value = "searchFile")
    public Map<String, Object> searchFile(@RequestBody SearchFileRequest requestBody, HttpServletRequest request){
        HttpSession session = request.getSession();
        int aid = Integer.parseInt(session.getAttribute(Account.aid.name()).toString());
        return fileService.searchFile(requestBody, aid);
    }

    @PostMapping(value = "fileDetail")
    public Map<String, Object> fileDetail(@RequestBody FileDetailRequest requestBody, HttpServletRequest request){
        HttpSession session = request.getSession();
        int aid = Integer.parseInt(session.getAttribute(Account.aid.name()).toString());
        return fileService.fileDetail(requestBody, aid);
    }

    @PostMapping(value = "fileRename")
    public Map<String, Object> fileRename(@RequestBody FileRenameRequest requestBody, HttpServletRequest request){
        HttpSession session = request.getSession();
        int aid = Integer.parseInt(session.getAttribute(Account.aid.name()).toString());
        return fileService.fileRename(requestBody, aid);
    }

    @PostMapping(value = "deleteFile")
    public Map<String, Object> deleteFile(@RequestBody FileDetailRequest requestBody, HttpServletRequest request){
        HttpSession session = request.getSession();
        int aid = Integer.parseInt(session.getAttribute(Account.aid.name()).toString());
        return fileService.deleteFile(requestBody, aid);
    }
}
